#
# SYNOPSIS
#
#   AX_CAPTAINBOL([force|yes|no|auto])
#
# DESCRIPTION
#
#   Searches for captainbol library with the --with... option.
#
#   If found, define HAVE_CAPTAINBOL and macros CAPTAINBOL_LIBS and CAPTAINBOL_CXXFLAGS.
#
#   The argument is used if no --[en|dis]able-protobuf option is set. Value "yes"
#   requires the configuration by default. Value "no" does not require it by
#   default. Value "auto" configures the library only if available.
#
# NOTE
#   This scripts takes into account dependencies on Boost::Serialize, Boost::Regex
#   and Google Protocole Buffer
#
# LAST MODIFICATION
#
#   2008-09-08
#
# COPYLEFT
#
#   Copyright (c) 2008 Ivan Topolsky <ivan.topolsky@genebio.com>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU Library General Public License as
#   published by the Free Software Foundation; either version 2 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public
#   License along with this program; if not, write to the
#   Free Software Foundation, Inc.,
#   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

# AX_PROTOBUF([force|yes|no|auto])
AC_DEFUN([AX_CAPTAINBOL],[

# enable / disable using 
  if test "x$1" = "xforce"; then
    ac_with_captainbol="yes"
  else
    AC_MSG_CHECKING([whether captainbol is enabled])
    AC_ARG_ENABLE([captainbol],[AC_HELP_STRING(
      [--enable-captainbol],[enable using captainbol (default:autodetect)])
AC_HELP_STRING(
      [--disable-captainbol],[disable using captainbol])],
    [
      AC_MSG_RESULT([$enableval])
      ac_with_captainbol=$enableval
    ],[
      AC_MSG_RESULT([$1])
      ac_with_captainbol=$1
    ])
  fi

# path to includes
  AC_ARG_WITH([captainbol-includes],[AC_HELP_STRING(
    [--with-captainbol-includes=<dir>],
    [include path for captainbol])],
  [
    ac_with_captainbol_includes="-I$withval"
  ])

# path to libs
  AC_ARG_WITH([captainbol-libs],[AC_HELP_STRING(
   [--with-captainbol-libs=<dir>],
   [library path for captainbol])],
  [
    ac_with_captainbol_libs="-L$withval"
  ])


  if test "$ac_with_captainbol" = "yes" -o "$ac_with_captainbol" = "auto" ; then


# step 1 : find includes
    oldCXXFLAGS="$CXXFLAGS"
    CAPTAINBOL_CXXFLAGS="$ac_with_captainbol_includes"
    CXXFLAGS="$oldCXXFLAGS $CAPTAINBOL_CXXFLAGS $PROTOBUF_CXXFLAGS $BOOST_CPPFLAGS"
    AC_CHECK_HEADERS([peak.h],[

# step 2 : find libs
      oldLIBS="$LIBS"
      CAPTAINBOL_LIBS="$ac_with_captainbol_libs -lcaptainbol-utils -lcaptainbol-score"
      LIBS="$oldLD $CAPTAINBOL_LIBS $PROTOBUF_LIBS $BOOST_LDFLAGS $BOOST_REGEX_LIB $BOOST_SERIALIZATION_LIB"
      AC_MSG_CHECKING([for bol::Peak::chargemask2string in library captainbol-utils])
      AC_LINK_IFELSE([AC_LANG_PROGRAM([#include "peak.h"],[cout << bol::Peak::chargemask2string(0)])],[
        AC_MSG_RESULT([yes])

# everything is OK !
          AC_DEFINE([HAVE_CAPTAINBOL],[1],[Captainbol library is available])
          HAVE_CAPTAINBOL=1
          AC_SUBST([HAVE_CAPTAINBOL])
          AC_SUBST([CAPTAINBOL_LIBS])
          AC_SUBST([CAPTAINBOL_CXXFLAGS])
        ],[
          AC_MSG_RESULT([no])
        ])
        LIBS="$oldLIBS"
      ])
      CXXFLAGS="$oldCXXFLAGS"

    if test "$ac_with_captainbol" = "yes" -a "x$HAVE_CAPTAINBOL" = "x" ; then
      AC_MSG_ERROR([cannot configure required CaptainBOL library.
Please try to specify location using '--with-captainbol-includes' and '--with-captainbol-libs'.
Also if your copy of CaptainBOL was compiled with support for Google Protocole Buffer, don't forget to enable it as well.])
# TODO add a comile test which test for "HAVE_PROTOBUF" in captainbol's config.h to know whethe or not protobuf support was compiled in.
    fi
  fi
])
